import os

os.environ['DEVICE_ID'] = '1'

# 配置昇腾相关环境变量
os.environ['TORCH_DEVICE_BACKEND_AUTOLOAD'] = '0'
os.environ['ASCEND_DEVICE_ID'] = '1'
os.environ['ASCEND_RT_VISIBLE_DEVICES'] = '1'
os.environ['ASCEND_VISIBLE_DEVICES'] = '1'
os.environ['ASCEND_HOME'] = '/usr/local/Ascend'

# 拼接 PATH 和 LD_LIBRARY_PATH（保留原有环境变量值）
os.environ['PATH'] = f"{os.environ['ASCEND_HOME']}/bin:{os.environ.get('PATH', '')}"
os.environ['LD_LIBRARY_PATH'] = f"{os.environ['ASCEND_HOME']}/lib64:{os.environ.get('LD_LIBRARY_PATH', '')}"

from dotenv import load_dotenv
from utils.loggingConfig import get_logger

logger = get_logger('')

root_path = os.path.dirname(os.path.abspath(__file__))
def __initEnv():
    env_path = os.path.join(root_path , 'env')
    env_path = os.path.abspath(env_path)
    load_dotenv(dotenv_path=env_path, verbose=True)
    for k , v in dict(os.environ).items():
        logger.info(f'{k} -> {v}')


def __init():
    __initEnv()

__init()

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from controller import modelExperienceController


def getApp():
    app = FastAPI()
    modelExperienceController.fapp = app
    # 解决跨域的问题
    app.add_middleware(
        CORSMiddleware,
        allow_origins=['*'],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # 注册路由
    app.include_router(modelExperienceController.router)

    return app